home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1999 Spring / macformat-077.iso / Shareware Plus / Development / SpriteWorld 2.2 / SpriteWorld Examples / Simple / Simple.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-25  |  9.3 KB  |  325 lines  |  [TEXT/CWIE]

  1. ///--------------------------------------------------------------------------------------
  2. // Simple.c
  3. ///--------------------------------------------------------------------------------------
  4.  
  5. #include <SWIncludes.h>            // Automatically include all SpriteWorld headers
  6. #include <SWGameUtils.h>
  7. #include <SWFPSReport.h>
  8.  
  9. #include "SWApplication.h"
  10. #include "Simple.h"
  11.  
  12.  
  13. #define kFullScreenWindow        false        // Set to true if you want a window larger than 640x480 
  14. #define kInterlacedMode            false        // You can use interlacing even with non-scrolling animations
  15. #define kSyncToVBL                true        // Syncs the animation to the VBL
  16. #define kWorldRectInset            0            // Makes the SpriteWorld smaller than the window.
  17. #define kMaxFPS                    0            // Keep the animation from going too fast
  18.  
  19. #define kNumSprites                10
  20. #define kMaxSpriteMoveDelta        8
  21.  
  22.  
  23. SpriteWorldPtr        gSpriteWorldP;
  24. SpriteLayerPtr        gSpriteLayerP;
  25. SpritePtr            gMasterBallSpriteP;
  26. WindowPtr            gWindowP;
  27.  
  28.  
  29. ///--------------------------------------------------------------------------------------
  30. // main
  31. ///--------------------------------------------------------------------------------------
  32.  
  33. void main(void)
  34. {
  35.     Initialize(kNumberOfMoreMastersCalls);
  36.     Randomize();
  37.  
  38.     if (SWHasSystem7())
  39.     {
  40.         CreateWindow();
  41.         SetUpSpriteWorld();
  42.         CreateBallSprite();
  43.         
  44.         AddSprites();
  45.         RunAnimation();
  46.         CleanUp();
  47.     }
  48.     else
  49.     {
  50.         CantRunOnThisMachine();
  51.     }
  52. }
  53.  
  54.  
  55. ///--------------------------------------------------------------------------------------
  56. //    CreateWindow
  57. ///--------------------------------------------------------------------------------------
  58.  
  59. void    CreateWindow(void)
  60. {
  61.     Rect        windRect;
  62.     RgnHandle    mBarUpdateRgn;
  63.     
  64.     gWindowP = GetNewCWindow(kWindowResID, NULL, (WindowPtr)-1L);
  65.  
  66.     if (gWindowP != NULL)
  67.     {
  68.         if (kFullScreenWindow == true)
  69.         {
  70.             SizeWindow(gWindowP, qd.screenBits.bounds.right, 
  71.                             qd.screenBits.bounds.bottom, false);
  72.             MoveWindow(gWindowP, 0, 0, false);
  73.         }
  74.         else
  75.         {
  76.             MoveWindow(gWindowP, 0, 0, false);
  77.             windRect = gWindowP->portRect;
  78.             
  79.                 // Make sure the window fits on the screen
  80.             if (windRect.bottom > qd.screenBits.bounds.bottom ||
  81.                  windRect.right > qd.screenBits.bounds.right)
  82.             {
  83.                     // Make it smaller so it fits on the screen
  84.                 SizeWindow(gWindowP, qd.screenBits.bounds.right, 
  85.                                 qd.screenBits.bounds.bottom, false);
  86.                 MoveWindow(gWindowP, 0, 0, false);
  87.             }
  88.             else
  89.             {
  90.                     // Center window in screen
  91.                 CenterRect(&windRect, &qd.screenBits.bounds);
  92.                 MoveWindow(gWindowP, windRect.left, windRect.top, false);
  93.             }
  94.         }
  95.         
  96.         ShowWindow(gWindowP);
  97.         SetPort(gWindowP);
  98.         mBarUpdateRgn = SWHideMenuBar(gWindowP); // Must be done *after* showing window!
  99.         EraseRgn(mBarUpdateRgn);
  100.     }
  101.     else
  102.         CantFindResource();
  103. }
  104.  
  105.  
  106. ///--------------------------------------------------------------------------------------
  107. //    SetUpSpriteWorld
  108. ///--------------------------------------------------------------------------------------
  109.  
  110. void    SetUpSpriteWorld(void)
  111. {
  112.     OSErr            err;
  113.     Rect            worldRect;
  114.     PixPatHandle    pixPatH;
  115.  
  116.     SetCursor(*GetCursor(watchCursor));
  117.  
  118.         // Initialize the SpriteWorld package
  119.     err = SWEnterSpriteWorld();
  120.     FatalError(err);
  121.     
  122.     worldRect = gWindowP->portRect;
  123.     InsetRect(&worldRect, kWorldRectInset, kWorldRectInset);
  124.     
  125.         // Create the SpriteWorld
  126.     err = SWCreateSpriteWorldFromWindow(&gSpriteWorldP, (CWindowPtr)gWindowP, 
  127.         &worldRect, NULL, 0);
  128.     FatalError(err);
  129.  
  130.         // Create the Sprite Layer
  131.     err = SWCreateSpriteLayer(&gSpriteLayerP);
  132.     FatalError(err);
  133.     
  134.         // Put the pieces together and lock the SpriteWorld. 
  135.         // Note: since we are locking the SpriteWorld before adding the sprites,
  136.         // we must make sure to lock each sprite individually when creating them.
  137.     SWAddSpriteLayer(gSpriteWorldP, gSpriteLayerP);
  138.     SWLockSpriteWorld(gSpriteWorldP);
  139.     
  140.     
  141.         // Use BlitPixie for the screen and offscreen DrawProcs.
  142.     if (gSpriteWorldP->pixelDepth == 8)        // 256 colors
  143.     {
  144.         if (kInterlacedMode)
  145.         {
  146.             SWSetSpriteWorldScreenDrawProc(gSpriteWorldP, BP8BitInterlacedRectDrawProc);
  147.             SWSetSpriteWorldOffscreenDrawProc(gSpriteWorldP, BP8BitInterlacedRectDrawProc);
  148.         }
  149.         else
  150.         {
  151.             SWSetSpriteWorldScreenDrawProc(gSpriteWorldP, BlitPixie8BitRectDrawProc);
  152.             SWSetSpriteWorldOffscreenDrawProc(gSpriteWorldP, BlitPixie8BitRectDrawProc);
  153.         }
  154.     }
  155.     else if ( !(SW_PPC && gSpriteWorldP->pixelDepth < 8) )    // not 256 colors
  156.     {
  157.         if (kInterlacedMode)
  158.         {
  159.             SWSetSpriteWorldScreenDrawProc(gSpriteWorldP, BPAllBitInterlacedRectDrawProc);
  160.             SWSetSpriteWorldOffscreenDrawProc(gSpriteWorldP, BPAllBitInterlacedRectDrawProc);
  161.         }
  162.         else
  163.         {
  164.             SWSetSpriteWorldScreenDrawProc(gSpriteWorldP, BlitPixieAllBitRectDrawProc);
  165.             SWSetSpriteWorldOffscreenDrawProc(gSpriteWorldP, BlitPixieAllBitRectDrawProc);
  166.         }
  167.     }
  168.  
  169.         // Draw the background
  170.     SWSetPortToBackground(gSpriteWorldP);
  171.     
  172.     if (gSpriteWorldP->pixelDepth == 1)
  173.         pixPatH = GetPixPat(129);        // B&W dithered background
  174.     else
  175.         pixPatH = GetPixPat(128);        // Color
  176.     
  177.     if (pixPatH != NULL)
  178.     {
  179.         FillCRect(&gSpriteWorldP->backRect, pixPatH);
  180.         DisposePixPat(pixPatH);
  181.     }
  182.     
  183.         // Set up other miscellaneous options
  184.     SWSetSpriteWorldMaxFPS(gSpriteWorldP, kMaxFPS);
  185.     SWSyncSpriteWorldToVBL(gSpriteWorldP, kSyncToVBL);
  186.     SWSetCleanUpSpriteWorld(gSpriteWorldP);
  187. }
  188.  
  189.  
  190. ///--------------------------------------------------------------------------------------
  191. //    CreateBallSprite - load the master ball sprite and prepare it for cloning later
  192. ///--------------------------------------------------------------------------------------
  193.  
  194. void    CreateBallSprite(void)
  195. {
  196.     OSErr    err;
  197.     
  198.     err = SWCreateSpriteFromCicnResource(gSpriteWorldP, &gMasterBallSpriteP, NULL, 
  199.             128, 1, kFatMask);
  200.     FatalError(err);
  201.  
  202.         // Set up the sprite (remember that it must be locked!)
  203.     SWLockSprite(gMasterBallSpriteP);
  204.     SWSetSpriteMoveBounds(gMasterBallSpriteP, &gSpriteWorldP->backRect);
  205.     SWSetSpriteMoveProc(gMasterBallSpriteP, BallSpriteMoveProc);
  206.     
  207.     if (gSpriteWorldP->pixelDepth == 8)
  208.     {
  209.         SWCompileSprite(gMasterBallSpriteP);
  210.         SWSetSpriteDrawProc(gMasterBallSpriteP, CompiledSprite8BitDrawProc);
  211.     }
  212.     else if ( !(SW_PPC && gSpriteWorldP->pixelDepth < 8) )
  213.     {
  214.         if (kInterlacedMode)
  215.             SWSetSpriteDrawProc(gMasterBallSpriteP, BPAllBitInterlacedMaskDrawProc);
  216.         else
  217.             SWSetSpriteDrawProc(gMasterBallSpriteP, BlitPixieAllBitMaskDrawProc);
  218.     }
  219. }
  220.  
  221.  
  222. ///--------------------------------------------------------------------------------------
  223. //    AddSprites - clone the master sprite, and add the clones to the SpriteWorld
  224. ///--------------------------------------------------------------------------------------
  225.  
  226. void    AddSprites(void)
  227. {
  228.     SpritePtr    newSpriteP;
  229.     short        spriteNum;
  230.     OSErr        err;
  231.     
  232.     for (spriteNum = 0; spriteNum < kNumSprites; spriteNum++)
  233.     {
  234.             // Clone the master sprite. The clone doesn't need to be locked,
  235.             // since the master sprite was already locked.
  236.         err = SWCloneSprite(gMasterBallSpriteP, &newSpriteP, NULL);
  237.         FatalError(err);
  238.         
  239.         SWAddSprite(gSpriteLayerP, newSpriteP);
  240.         
  241.         SWSetSpriteLocation(newSpriteP,
  242.                 GetRandom(0, gSpriteWorldP->backRect.right-44), 
  243.                 GetRandom(0, gSpriteWorldP->backRect.bottom-44) );
  244.         
  245.         do
  246.         {
  247.             SWSetSpriteMoveDelta(newSpriteP,
  248.                     GetRandom(-kMaxSpriteMoveDelta, kMaxSpriteMoveDelta), 
  249.                     GetRandom(-kMaxSpriteMoveDelta, kMaxSpriteMoveDelta));
  250.         } while (newSpriteP->horizMoveDelta == 0 || newSpriteP->vertMoveDelta == 0);
  251.     }
  252. }
  253.  
  254.  
  255. ///--------------------------------------------------------------------------------------
  256. //    RunAnimation
  257. ///--------------------------------------------------------------------------------------
  258.  
  259. void    RunAnimation(void)
  260. {
  261.     unsigned long    frames;
  262.     
  263.     SetCursor(&qd.arrow);
  264.     HideCursor();
  265.     
  266.         // Make sure CopyBits, if used, doesn't try to colorize things
  267.     SWSetPortToWindow(gSpriteWorldP);
  268.     ForeColor(blackColor);
  269.     BackColor(whiteColor);
  270.     
  271.     SWUpdateSpriteWorld(gSpriteWorldP, true);
  272.     
  273.     FatalError( SWStickyError() ); // Make sure no errors got past us during setup
  274.     
  275.     frames = 0;
  276.     StartTimer();    // For FPS report later
  277.  
  278.     while (!Button())
  279.     {
  280.         SWProcessSpriteWorld(gSpriteWorldP);
  281.         FatalError( SWStickyError() );    // Make sure no errors occurred during a MoveProc, etc.
  282.         SWAnimateSpriteWorld(gSpriteWorldP);
  283.         
  284.         if (gSpriteWorldP->frameHasOccurred)
  285.             frames++;        
  286.     }
  287.     
  288.     SWShowMenuBar((WindowPtr)gWindowP);
  289.     ShowResults(frames);    // Show FPS report
  290. }
  291.  
  292.  
  293. ///--------------------------------------------------------------------------------------
  294. //     CleanUp - This function is not really necessary, since the system will dispose
  295. //    everything automatically when the program quits. However, if you sync the animation
  296. //    to the VBL, you must either call SWDisposeSpriteWorld, or SWSyncSpriteWorldToVBL
  297. //    with a value of false before your program quits, in order to remove the VBL task.
  298. ///--------------------------------------------------------------------------------------
  299.  
  300. void    CleanUp(void)
  301. {
  302.         // Dispose the master sprite, since it isn't included in the SpriteWorld
  303.     SWDisposeSprite(&gMasterBallSpriteP);
  304.     
  305.         // Dispose the SpriteWorld, including all sprites and layers currently in it
  306.     SWDisposeSpriteWorld(&gSpriteWorldP);
  307.     SWExitSpriteWorld();
  308.     
  309.     FlushEvents(everyEvent, 0);
  310.     ShowCursor();
  311. }
  312.  
  313.  
  314. ///--------------------------------------------------------------------------------------
  315. //  BallSpriteMoveProc
  316. ///--------------------------------------------------------------------------------------
  317.  
  318. SW_FUNC void BallSpriteMoveProc(SpritePtr ballSpriteP)
  319. {    
  320.     SWOffsetSprite(ballSpriteP, ballSpriteP->horizMoveDelta, ballSpriteP->vertMoveDelta);
  321.     
  322.     (void)SWBounceSprite(ballSpriteP);
  323.     //(void)SWWrapSprite(ballSpriteP);
  324. }
  325.